Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Python float

Float Datatype

✦ The float type in Python represents a floating-point number. ✦ Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. ✦ For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers. ✦ Python float values are represented as 64-bit double-precision values. ✦ The maximum value any floating-point number can be is approximately 1.8 x 10^308. Any number greater than this will be indicated by the string inf in Python. ✦ Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. ✦ Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine. ✦ The float type implements the numbers.Real abstract base class.

Python Methods for float objects:

float.as_integer_ratio(): Returns a pair of integers whose ratio is exactly equal to the actual float having a positive denominator. ✦ float.is_integer(): Returns True if the float instance is finite with integral value, else, False. ✦ float.hex(): Returns a representation of a floating-point number as a hexadecimal string. ✦ float.fromhex(s): Returns the float represented by a hexadecimal string s.
python float example - Basic float examples in python # Initialize a float num = 10.5 print(num) # Declare a float with scientific notation num_sci = 1.5e2 print(num_sci) #printing type print(type(num))

Output

10.5 150.0 <class'float'>
python float example with different values in python # Declare a float with a negative value neg_num = -10.5 print(neg_num) # Declare a float with zero zero_num = 0.0 print(zero_num) # Declare a float with a very small value (close to zero) small_num = 1e-6 print(small_num) # Declare a float with a very large value large_num = 1.2e34 print(large_num)

Output

-10.5 0.0 1e-06 1.2e+34
Remember, Python uses the float data type to improve the readability of code and make it consistent across the wide spectrum of Python code. Consistency within one module or function is the most important.

  📌TAGS

★python ★ datatypes ★ float

Tutorials